package com.tinyideatech.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tinyideatech.system.entity.MallGoods;
import com.tinyideatech.system.mapper.MallGoodsMapper;
import com.tinyideatech.system.service.IMallGoodsService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangy-5
 * @date 2019-08-26
 */
@Service
public class MallGoodsServiceImpl extends ServiceImpl<MallGoodsMapper, MallGoods> implements IMallGoodsService {

    @Override
    public void changeStatus(MallGoods mallGoods) {

        MallGoods goods = getById(mallGoods.getGoodsId());
        if (goods != null) {
            goods.setStatus(mallGoods.getStatus());
            updateById(goods);
        }
    }

    @Override
    public int countModel(long id) {
        QueryWrapper<MallGoods> qw = new QueryWrapper<>();
        qw.eq("classify_id", id);
        return super.count(qw);
    }

    @Override
    public List<MallGoods> searchList(String keywords, String recommend, String category) {
        QueryWrapper<MallGoods> qw = new QueryWrapper<>();
        if (keywords.equals("")) {
            if (!recommend.equals("") && category.equals("")) {
                qw.eq("top", recommend);
            }
            if (recommend.equals("") && !category.equals("")) {
                qw.eq("classify_name", category);
            }
            if (!recommend.equals("") && !category.equals("")) {
                qw.eq("classify_name", category).eq("top", recommend);
            }
        }
        if (!keywords.equals("")) {
            if (!recommend.equals("") && category .equals("")) {
                qw.eq("top", recommend).like("goods_name", keywords);
            }
            if (recommend.equals("") && !category.equals("")) {
                qw.eq("classify_name", category).like("goods_name", keywords);
            }
            if (recommend.equals("") && category.equals("")) {
                qw.eq("goods_name", keywords);
            }
            if (!category.equals("") && !recommend.equals("")) {
                qw.eq("top", recommend).eq( "classify_name", category).like("goods_name", keywords);
            }
        }
        return super.list(qw);
    }
}
